// miniprogram/pages/admin/setting/setting.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {

    
    studio: {},
    tabindex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    // this.getSetiings()
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      if (user.studio) {
        that.setData({
          studio: user.studio,
          settings: app.globalData.settings
        })
      }
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  apply: function (e) {
    console.log(e)
    let studio = this.data.studio;
    studio.cover.list = this.selectComponent('#dragger').data.items;
    studio.cover.height = e.detail.value.coverheight
    studio.theme.portfolio_index_sum = e.detail.value.portfolio_index_sum
    studio.theme.service_index_sum = e.detail.value.service_index_sum
    studio.theme.portfolio_title_name = e.detail.value.portfolio_title_name
    studio.theme.service_title_name = e.detail.value.service_title_name
    this.saveSetting(studio);
  },

  saveSetting(studio) {
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        op: 'update',
        table: 'studio',
        data: studio
      },
      success: res => {
        wx.showToast({
          title: '数据保存成功',
        })

        let pages = getCurrentPages();
        if (res.result._id || res.result.stats.updated) {
          if (pages.length > 1) {
            pages[pages.length - 2].onLoad(pages[pages.length - 2].options);
          }
          wx.navigateBack();
        }

        //  wx.navigateBack()
      }
    })
  },
  changeTab(e) {
    let tabindex = e.currentTarget.dataset.index
    console.log(tabindex)
    this.setData({
      tabindex
    })
  },
  changeTemplate(e) {
    let name = e.currentTarget.dataset.name
    let template = e.currentTarget.dataset.index
    this.setData({
      [`studio.theme.${name}`]: template
      // 'studio.theme.template': template,
    })
  },
  changeColor(e) {
    let mainColor = e.currentTarget.dataset.color
    console.log(this.brightnessOfColor(mainColor))

    let fontColor = this.brightnessOfColor(mainColor) < 200 ? '#fff' : '#000'
    this.setData({
      'studio.theme.mainColor': mainColor,
      'studio.theme.fontColor': fontColor
    })

  },

  hexToRgb(hexColor) {
    return parseInt(hexColor, 16).toString();
  },
  brightnessOfColor(colorHexdecimalValue) {
    let useColorValue = colorHexdecimalValue.slice(1);
    let rColor = useColorValue.slice(0, 2);
    let gColor = useColorValue.slice(2, 4);
    let bColor = useColorValue.slice(4);

    let rColorValue = this.hexToRgb(rColor);
    let gColorValue = this.hexToRgb(gColor);
    let bColorValue = this.hexToRgb(bColor);

    let lightness = rColorValue * 0.299 + gColorValue * 0.587 + bColorValue * 0.114;
    return lightness;
  },
  chooseCoverImg: function () {
    const that = this;
    wx.navigateTo({
      url: '/pages/admin/gallary/gallary',
      success: res => {
        res.eventChannel.on('setImages', items => {
          let cover = this.data.studio.cover;
          if (!Array.isArray(cover)) cover = [];
          items.map(v => {
            cover.push(v);
          })
          that.setData({
            ['studio.cover']: cover
          });
        });
        let cover = this.data.studio.cover;
        if (!cover) cover = [];
        res.eventChannel.emit('setImages', {
          count: 9 - cover.length,
          items: cover
        });
      }
    })
  },


  removeCover(e) {
    const that = this;
    let index = e.currentTarget.dataset.index
    let cover = this.data.studio.cover;

    cover.splice(index, 1)
    that.setData({
      'studio.cover': cover
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})